package com.beiding.jsonanalyzer.analyze.query;

import com.beiding.jsonanalyzer.analyze.func.FuncBase;
import com.beiding.jsonanalyzer.flow.ValueNode;

/**
 * 简单比较
 *
 * @author 丁常磊
 * @date 2022/6/10 14:09
 */

public abstract class SimpleCondition implements FuncBase<ComparatorResult> {

    /**
     * 函数
     */
    private FuncBase[] functions;

    public SimpleCondition(FuncBase... functions) {
        this.functions = functions;
    }

    @Override
    public boolean checkAgg() {
        for (FuncBase function : functions) {
            if (function.checkAgg()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public ComparatorResult executeOne(ValueNode valueNode) {
        Object[] values = new Object[functions.length];
        for (int i = 0; i < functions.length; i++) {
            values[i] = functions[i].executeOne(valueNode);
        }
        return compare(values);
    }

    @Override
    public void pugAgg(ValueNode valueNode) {
        for (FuncBase function : functions) {
            function.pugAgg(valueNode);
        }
    }

    @Override
    public void resetAgg() {
        for (FuncBase function : functions) {
            function.resetAgg();
        }
    }

    @Override
    public ComparatorResult aggGet() {
        Object[] values = new Object[functions.length];
        for (int i = 0; i < functions.length; i++) {
            values[i] = functions[i].aggGet();
        }
        return compare(values);
    }

    /**
     * 执行比较
     * @param values
     * @return
     */
    protected abstract ComparatorResult compare(Object[] values);

}
